#include <windows.h>
#include <bitree.h>

#include <cstdlib>
#include <cstdio>

void setup() {
    // 设置控制台代码页为UTF-8
    SetConsoleOutputCP(CP_UTF8);
    // 设置控制台输入代码页为UTF-8
    SetConsoleCP(CP_UTF8);
    setvbuf(stdout, NULL, _IONBF, 0); // 设置为无缓冲模式
}

int main() {
    setup();
    BiTree T = NULL;
    BiTree T2 = NULL;
    int create_choice;

    printf("请选择创建二叉树的方式：\n1:完整节点输入\n2:前序+中序\n3:中序+后序\n选择：");
    scanf("%d", &create_choice);
    fflush(stdin);

    if (create_choice == 1) {
        printf("输入完整节点序列（#表示空节点）：");
        BiTree_Create(T);
    } else if (create_choice == 2) {
        char Pre[100], In[100];
        int cnt = 0;
        printf("输入前序序列：");
        char c;
        while ((c = getchar()) != '\n' && cnt < 99) Pre[cnt++] = c;
        Pre[cnt] = '\0';

        cnt = 0;
        printf("输入中序序列：");
        while ((c = getchar()) != '\n' && cnt < 99) In[cnt++] = c;
        In[cnt] = '\0';
        T = BiTree_PICreate(Pre, In, 0, 0, cnt);
    } else if (create_choice == 3) {
        char In[100], Post[100];
        int cnt = 0;
        printf("输入中序序列：");
        char c;
        while ((c = getchar()) != '\n' && cnt < 99) In[cnt++] = c;
        In[cnt] = '\0';

        cnt = 0;
        printf("输入后序序列：");
        while ((c = getchar()) != '\n' && cnt < 99) Post[cnt++] = c;
        Post[cnt] = '\0';
        T = BiTree_IPCreate(In, Post, 0, 0, cnt);
    }

    int choice;
    do {
        printf("\n1-先序遍历\n2-中序遍历\n3-后序遍历\n4-叶子数\n5-深度\n6-复制\n7-判等\n8-查找\n9-路径\n10-退出\n选择：");
        scanf("%d", &choice);
        fflush(stdin);

        switch(choice) {
            case 1:
                printf("前序：");
                BiTree_PrePrint(T);
                printf("\n");
                break;
            case 2:
                printf("中序：");
                BiTree_InPrint(T);
                printf("\n");
                break;
            case 3:
                printf("后序：");
                BiTree_PostPrint(T);
                printf("\n");
                break;
            case 4: {
                int cnt = 0;
                BiTree_CountLeafNode(T, cnt);
                printf("叶子数：%d\n", cnt);
                break;
            }
            case 5:
                printf("深度：%d\n", BiTree_Depth(T));
                break;
            case 6:
                BiTree_Copy(T, T2);
                printf("复制完成\n");
                break;
            case 7:
                printf("相等性：%d\n", BiTree_PreCompare(T, T2));
                break;
            case 8: {
                printf("查找值：");
                char val;
                scanf("%c", &val);
                fflush(stdin);
                char tmp[100];
                BiTree res = BiTree_SearchNode(T, val, tmp);
                printf(res ? "存在，路径为：%s\n" : "不存在\n", tmp);
                break;
            }
            case 9: {
                printf("目标值：");
                char val;
                scanf("%c", &val);
                fflush(stdin);
                char path[100] = {0};
                BiTree_SearchNode(T, val, path);
                printf("路径：%s\n", path);
                break;
            }
        }
    } while(choice != 10);

    return 0;
}